package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.vo.SocialVO;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.RegisterMemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author lihaojie
 * @email lihaojie@qq.com
 * @date 2023-11-03 20:27:55
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 微博社交登录接口
     * @param socialVO token信息
     * @return r
     */
    @PostMapping("/social/login")
    public R socialLogin(@RequestBody SocialVO socialVO) {
        return memberService.socialLogin(socialVO);
    }

    /**
     * 登录接口
     * @param memberLoginVo 登录数据
     * @return  r
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity member = memberService.login(memberLoginVo);
        if (member == null) {
            return R.error(BizCodeEnum.MEMBER_LOGIN_ACCOUNT_PASSWORD_ERROR.getCode(), BizCodeEnum.MEMBER_LOGIN_ACCOUNT_PASSWORD_ERROR.getMsg());
        }
        return R.ok(member);
    }

    /**
     * 用户注册
     * @param registerMemberVo 注册内容
     * @return r
     */
    @PostMapping("/register")
    public R registerMember(@RequestBody RegisterMemberVo registerMemberVo) {
        try {
            memberService.registerMember(registerMemberVo);
            return R.ok();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
